package cn.charmaine.basic.base.controller;

import cn.charmaine.basic.Exception.CustomException;
import cn.charmaine.basic.base.service.ILoginService;
import cn.charmaine.basic.util.AjaxResult;
import cn.charmaine.org.domain.dto.LoginDto;
import cn.charmaine.org.domain.dto.WeChatDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 管理员与用户共用一个，用type区分，0为管理员，1为用户
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @PostMapping
    public AjaxResult login(@RequestBody LoginDto LoginDto){
        try {
            return AjaxResult.thisSuccess().setResultObj(loginService.login(LoginDto));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.thisSuccess().setSuccess(false).setMessage(e.getMessage());
        }
    }


    @PostMapping("/wechat")
    private AjaxResult wechat(@RequestBody Map<String,String> map){
        try {
            return loginService.wechat(map);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.thisSuccess().setSuccess(false).setMessage(e.getMessage());
        }

    }

    @PostMapping("/wechat/binder")
    private AjaxResult binder(@RequestBody WeChatDto weChatDto){
        try {
            return loginService.binder(weChatDto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.thisSuccess().setSuccess(false).setMessage(e.getMessage());
        }
    }

}
